'user strict'

module.exports = function (grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
      options: {
        livereload: true
      },
      gruntfile:{
        files: 'Gruntfile.js',
        options: {
          reload: true
        }
      },
      jade: {
        tasks: "jade",
        files: ["**/*.jade", "!layouts/*.jade"],
      },
      less: {
        tasks: "less",
        files: "./dev/less/*.less",
      },
      coffee: {
        tasks: ["coffee"],
        files: ["./dev/coffee/*.coffee"]
      }
    },
    jade: {
      options: {
        pretty: true,
        bare: true,
        files: {
          "*": ["**/*.jade", "!layouts/*.jade"]
        }
      },
      dev: {
        options: {
          bare: true,
          locals: {
            livereload: true
          }
        },
        files: [
          {
            "index.html": 'dev/jade/index.jade'
          },
          {
            expand: true,
            cwd: './dev/jade/',
            src: '*.jade',
            dest: './html/',
            ext: '.html'
          }
        ]
      }
    },
    coffee: {
      compile: {
        options:{
          bare: true
        },
        files: [
          {
            expand: true,
            cwd: './dev/coffee/',
            src: '*.coffee',
            dest: './script/',
            ext: '.js'
          }
        ]
      }
    },
    less: {
      compile: {
        files: [
          {
            expand: true,
            cwd: './dev/less/',
            src: '*.less',
            dest: './css/',
            ext: '.css'
          }
        ]
      }
    },
    uglify:{
      dist: {
        files:{
          './static/dist/js/index.js': './static/js/**.js'
        }
      }
    },
    web: {
      options: {
        port: 9909
      }
    },
  });

  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.loadNpmTasks('grunt-contrib-coffee');


  grunt.registerTask('web', 'Start web server...', function () {
    var options = this.options();
    var express = require('express');
    var app = express()
    app.use(express.static(__dirname + '/public')).listen(options.port)
    console.log('http://localhost:%s', options.port);

    grunt.task.run("watch")
  });

  grunt.registerTask('default', ['jade', 'coffee', 'less', 'web'])
}
